<template>
  <div class="root">
    <van-nav-bar :title="houseInfo.community" :fixed="true" left-arrow @click-left="$router.go(-1)"/>
    <!-- 底部区域 -->
    <div class="bottom">
      <div @click="favorite">
        <img
          :src="isFavorite?'http://localhost:8080/img/star.png':'http://localhost:8080/img/unstar.png'"
          class="Detail_favoriteImg__2xUna"
          alt="收藏"
        >
        <span>{{isFavorite?'已收藏':'收藏'}}</span>
      </div>
      <div>在线咨询</div>
      <div>电话预约</div>
    </div>
    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item class="my-swipe" v-for="(image, index) in houseInfo.houseImg" :key="index">
        <img class="my-swipe" :src="url+image">
      </van-swipe-item>
    </van-swipe>
    <!-- 信息 -->
    <div class="Detail_info__Z0IwB">
      <h3 class="Detail_infoTitle__1NjGn">{{houseInfo.title}}</h3>
      <div class="am-flexbox Detail_tags__2g553 am-flexbox-align-start">
        <div class="am-flexbox-item">
          <span
            :key="index"
            v-for="(tag,index) in houseInfo.tags"
            :class="['Detail_tag__2Mpog', 'tag'+(index+1)]"
          >{{tag}}</span>
        </div>
      </div>
      <div class="am-flexbox Detail_infoPrice__1P_cv am-flexbox-align-center">
        <div class="am-flexbox-item Detail_infoPriceItem__2WiUY">
          <div>
            {{houseInfo.price}}
            <span class="Detail_month__taVYT">/月</span>
          </div>
          <div>租金</div>
        </div>
        <div class="am-flexbox-item Detail_infoPriceItem__2WiUY">
          <div>{{houseInfo.roomType}}</div>
          <div>房型</div>
        </div>
        <div class="am-flexbox-item Detail_infoPriceItem__2WiUY">
          <div>{{houseInfo.size}}</div>
          <div>面积</div>
        </div>
      </div>
      <div class="am-flexbox Detail_infoBasic__tBuVh am-flexbox-align-start">
        <div class="am-flexbox-item">
          <div class="abc">
            <span class="Detail_title__3fq76">装修：</span>精装
          </div>
          <div class="abc">
            <span class="Detail_title__3fq76">楼层：</span>
            {{houseInfo.floor}}
          </div>
        </div>
        <div class="am-flexbox-item">
          <div class="abc">
            <span class="Detail_title__3fq76">朝向：</span>
            {{houseInfo.floor}}
          </div>
          <div class="abc">
            <span class="Detail_title__3fq76">类型：</span>普通住宅
          </div>
        </div>
      </div>
    </div>
    <!-- 地图 -->
    <div class="map">
      <div class="Detail_mapTitle__3tDr5">
        小区：
        <span>{{houseInfo.community}}</span>
      </div>
      <div id="container"></div>
    </div>
    <div class="Detail_about__hnyUW">
      <div class="Detail_houseTitle__2xpe_">房屋配套</div>
      <HousePackage v-if="houseInfo.houseCode" :data="houseInfo.supporting"/>
    </div>
    <!-- 房屋概况 -->
    <div class="Detail_set__2IQSS">
      <div class="Detail_houseTitle__2xpe_">房源概况</div>
      <div>
        <div class="Detail_contact__3lQvy">
          <div class="Detail_user__2eB6N">
            <img src="http://localhost:8080/img/avatar.png" alt="头像">
            <div class="Detail_useInfo__3I74z">
              <div>王女士</div>
              <div class="Detail_userAuth__3ZMPG">
                <i class="iconfont icon-auth"></i>已认证房主
              </div>
            </div>
          </div>
          <span class="Detail_userMsg__2jpnu">发消息</span>
        </div>
        <div class="Detail_descText__3W3sv">{{houseInfo.description}}</div>
      </div>
    </div>
    <!-- 推荐 -->
    <div class="Detail_recommend__O3lzG">
      <div class="Detail_houseTitle__2xpe_">猜你喜欢</div>
      <div class="Detail_items__1K5hJ">
        <HouseItem v-for="(item,index) in recommendHouses" :key="index" :item="item"/>
      </div>
    </div>
  </div>
</template>

<script>
import { request, setToken, url, isAu } from "../utils/index.js";
import HousePackage from "../components/HousePackage";
import HouseItem from "../components/HouseItem";
const BMapGL = window.BMapGL;

const labelStyle = {
  position: "absolute",
  zIndex: -7982820,
  backgroundColor: "rgb(238, 93, 91)",
  color: "rgb(255, 255, 255)",
  height: 25,
  padding: "5px 10px",
  lineHeight: "14px",
  borderRadius: 3,
  boxShadow: "rgb(204, 204, 204) 2px 2px 2px",
  whiteSpace: "nowrap",
  fontSize: 12,
  userSelect: "none"
};
const recommendHouses = [
  {
    id: 1,
    houseImg: "/img/message/1.png",
    desc: "72.32㎡/南 北/低楼层",
    title: "安贞西里 3室1厅",
    price: 4500,
    tags: ["随时看房"]
  },
  {
    id: 2,
    houseImg: "/img/message/2.png",
    desc: "83㎡/南/高楼层",
    title: "天居园 2室1厅",
    price: 7200,
    tags: ["近地铁"]
  },
  {
    id: 3,
    houseImg: "/img/message/3.png",
    desc: "52㎡/西南/低楼层",
    title: "角门甲4号院 1室1厅",
    price: 4300,
    tags: ["集中供暖"]
  }
];
export default {
  components: {
    HousePackage,
    HouseItem
  },
  data() {
    return {
      // 数据加载中状态
      url: url,
      isLoading: false,
      // 房屋详情
      houseInfo: {
        // 房屋图片
        houseImg: [],
        // 标题
        title: "",
        // 标签
        tags: [],
        // 租金
        price: 0,
        // 房型
        roomType: "",
        // 房屋面积
        size: 0,
        // 朝向
        oriented: [],
        // 楼层
        floor: "",
        // 小区名称
        community: "",
        // 地理位置
        coord: {
          latitude: "39.928033",
          longitude: "116.529466"
        },
        // 房屋配套
        supporting: [],
        // 房屋标识
        houseCode: "",
        // 房屋描述
        description: ""
      },
      // 表示房源是否收藏
      isFavorite: false,
      houseCode: this.$route.params.id,
      recommendHouses,
      isLoaded: false,
      isAu: isAu()
    };
  },
  async created() {
    const res = await request.get(`/houses/${this.houseCode}`);
    this.houseInfo = res.data.body;
    this.createdMap();
    this.isLoaded = true;
    this.getFavorite();
  },
  mounted() {},
  methods: {
    createdMap() {
      var map = new BMapGL.Map("container"); // 创建Map实例

      // 初始化地图,设置中心点坐标和地图级别
      var point = new BMapGL.Point(
        this.houseInfo.coord.longitude,
        this.houseInfo.coord.latitude
      );
      var label = new BMapGL.Label("", {
        // 创建文本标注
        position: point,
        offset: new BMapGL.Size(0, -36)
      });
      // 自定义文本标注样式
      label.setContent(`<div>
            <span>${this.houseInfo.community}</span>
           <div class="mapArrow"></div>
    </div > `);
      label.setStyle(labelStyle);
      map.addOverlay(label); // 将标注添加到地图中
      map.centerAndZoom(point, 17);
    },
    async favorite() {
      if (!this.isAu) {
        this.$dialog
          .confirm({
            title: "登录",
            message: "该功能需要先登录，是否登录"
          })
          .then(() => {
            this.$router.push("/user/login");
          })
          .catch(() => {
            // on cancel
          });
      } else {
        if (this.isFavorite) {
          const res = await request.delete(`/user/favorites/${this.houseCode}`);
          if (res.data.status === 200) {
            this.isFavorite = false;
          } else {
            this.$toast.fail("操作失败");
          }
        } else {
          const res = await request.post(`/user/favorites/${this.houseCode}`);
          if (res.data.status === 200) {
            this.isFavorite = true;
          } else {
            this.$toast.fail("操作失败");
          }
        }
      }
    },
    // 判断是否收藏
    async getFavorite() {
      if (!this.isAu) {
        this.isFavorite = false;
      } else {
        const res = await request.get(`/user/favorites/${this.houseCode}`);
        if (res.data.status === 400) {
          this.isAu = false;
          this.isFavorite = false;
        } else {
          this.isFavorite = res.data.body.isFavorite;
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
.root {
  padding: 46px 0 50px 0;
}
/deep/ .van-swipe {
  .my-swipe {
    width: 375px;
    height: 211px;
  }
}
.Detail_info__Z0IwB {
  padding: 15px;
  background: #fff;
  .Detail_infoTitle__1NjGn {
    font-weight: normal;
    font-size: 16px;
    color: #333;
  }
  .Detail_tags__2g553 {
    display: flex;
    position: relative;
    margin: 10px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid #cccccc;
    .am-flexbox-item {
      height: 50px;
      padding: 0 15px;
      display: flex;
      justify-content: center;
      align-items: center;
      .Detail_tag__2Mpog {
        display: inline-block;
        font-size: 12px;
        border-radius: 3px;
        padding: 4px 5px;
        margin-right: 5px;
        line-height: 12px;
      }
      .tag1 {
        color: #39becd;
        background: #e1f5f8;
      }
      .tag2 {
        color: #3fc28c;
        background: #e1f5ed;
      }
      .tag3 {
        color: #5aabfd;
        background: #e6f2ff;
      }
      .tag4 {
        color: #ff009d;
        background: #bda7fc;
      }
      .tag5 {
        color: #00ff80d7;
        background: #2fdd72;
      }
      .tag6 {
        color: #ae00ff;
        background: #3f93a0;
      }
    }
  }
  .Detail_infoPrice__1P_cv {
    border-top: solid 1px #cecece;
    border-bottom: solid 1px #cecece;
    margin: 15px 0;
    padding: 15px 0;
    display: flex;
    justify-content: space-between;
  }
  .Detail_infoPriceItem__2WiUY {
    display: flex;
    height: 56px;
    padding: 0 15px;
    justify-content: center;
    align-items: center;
  }
  .Detail_infoBasic__tBuVh {
    margin-top: 15px;
    border-bottom: solid 1px #cecece;
    display: flex;
    font-size: 13px;
    .am-flexbox-item {
      display: flex;
      height: 56px;
      padding: 15px 0;
      color: #333333;
      .abc {
        margin-right: 5px;
      }
      span {
        color: #999999;
      }
    }
  }
}
.Detail_infoPriceItem__2WiUY div:first-child {
  color: #fa5741;
  font-size: 18px;
  font-weight: bolder;
}
/deep/ .map {
  height: 189px;
  margin-top: 10px;
  background: #ffffff;
  .Detail_mapTitle__3tDr5 {
    margin: 0 3%;
    line-height: 44px;
    color: #666;
    span {
      color: #333;
    }
  }
  #container {
    width: 100%;
    height: 145px;
    .mapArrow {
      background: url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png)
        no-repeat !important;
      position: absolute !important;
      width: 11px !important;
      height: 10px !important;
      top: 23px !important;
      left: 10px !important;
      overflow: hidden !important;
    }
  }
}
/deep/ .Detail_houseTitle__2xpe_ {
  height: 48px;
  padding: 15px 0;
  font-weight: 600;
  margin: 0 15px;
  border-bottom: 1px solid #cccccc;
}
.Detail_set__2IQSS {
  .Detail_houseTitle__2xpe_ {
    margin: 0;
    font-weight: 600;
    font-size: 16px;
  }
  -webkit-text-size-adjust: 100%;
  font-size: 14px;
  color: #333;
  font-family: "Microsoft YaHei";
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  box-sizing: border-box;
  touch-action: pan-y;
  margin: 10px 0px;
  padding: 0 15px;
  line-height: 150%;
  background: #fff;
  .Detail_contact__3lQvy {
    position: relative;
    height: 62px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 30px;
  }
  .Detail_user__2eB6N {
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 52px;
      height: 52px;
      margin-right: 10px;
    }
    .Detail_useInfo__3I74z {
      .Detail_userAuth__3ZMPG {
        color: #fa5741;
      }
    }
  }
  .Detail_userMsg__2jpnu {
    position: absolute;
    right: 15px;
    top: 20px;
    color: #33be85;
    border: solid 1px #33be85;
    border-radius: 3px;
    padding: 3px 15px;
  }
  .Detail_descText__3W3sv {
    line-height: 1.6;
    padding: 10px 0;
    color: #333;
  }
}
.bottom {
  z-index: 100;
  position: fixed;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 50px;
  background: rgb(255, 255, 255);
  display: flex;
  font-size: 17px;
  div {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 33%;
    height: 100%;
    font-size: 17px;
    color: #999999;
  }
  img {
    width: 16px;
    margin-right: 5px;
    margin-bottom: 2px;
  }
}
.bottom :nth-child(3) {
  background-color: #21b97a;
  color: #ffffff;
}
</style>
